<template>
	<view class="page_main page_render" :class="{ can_show: render_req }">
		<pagelogin ref="pagelogin" @loginready="pagerefresh" @initcallback="com_pagelogin_initcallback($event)" need_login="0">
			<block v-if="objJudge(PAGE_VIEW)">
				<diynav ref="diynav" :navstyle="{ background: '#383950' }" isFixed isdiy hasHeight @settop="set_pagetop($event)">
					<template slot="diy">
						<view class="index_message">
							<view class="left_view" @click="com_page_back()">
								<image :src="`/static/images/top_back_${color_mode}.png`" class="left_icon_img"></image>
							</view>

							<view class="title">
								{{ PAGE_VIEW[page_type].line_tt }}
							</view>

							<view class="right_view"></view>
						</view>
					</template>
				</diynav>

				<view class="page_setting" :style="{ paddingBottom: `${COMMON_VAR.safeAreaInsets_bot}rpx` }">
					<myToolList
						pagetype="setting_detail"
						:showForm="PAGE_VIEW[page_type].view_list"
						:keyForm="BLUETOOTH_INIT_FORM"
						@change="COMMAND_TRANSFER($event)"
						@switch="COMMAND_TRANSFER($event)"
						@formClick="myToolsClick($event)"
					></myToolList>
				</view>
			</block>

			<mypopup
				:on="pagepopup"
				:popup_type="2"
				ani_position="center"
				ani_type="scale"
				:popup_config="popup_config"
				@popup_btn_click="popup_click($event)"
				@mask_click="hidepopup()"
			></mypopup>
		</pagelogin>
	</view>
</template>

<script>
import storeMixin from '@/store/storeMixin';
export default {
	mixins: [storeMixin],
	data() {
		return {
			color_mode: 'dark',
			change_var: {},
			scrollTop: 0,
			render_component_init: false,
			page_render: false,
			page_title: '',
			render_req: false,
			nextreq: true,
			page_id: 0,
			page_form: {},
			page_list: [],
			loadingstatus: 0,
			page_type: '',
			PAGE_VIEW_KEY: 'PAGE_SETTING_DETAIL',
			PAGE_VIEW: {},
			pagepopup: false,
			popup_config: {},
		};
	},
	onLoad(v) {
		this.page_type = v.type;
		this.init_pagedata();
	},
	onShow() {
		this.com_onshow_fun();
	},
	onHide() {
		this.com_page_hide(this);
	},
	onPageScroll(e) {
		this.scrollTop = e.scrollTop;
	},
	onPullDownRefresh() {
		this.pagerefresh(1);
	},
	onShareAppMessage(res) {},
	onShareTimeline(res) {},
	methods: {
		//页面刷新
		pagerefresh(type = 0) {
			let _this = this;
			_this.startPulldownJudge(type);
			_this.com_page_show(_this);
			_this.com_loading_fun();
			_this.INIT_PAGE_COMMADN_VIEW(_this.PAGE_VIEW_KEY);
			_this.init_form();
		},
		// 读取数据
		init_form() {
			let _this = this;
			let object = this.PAGE_VIEW;
			// 缓存的数据
			let cache_config = uni.getStorageSync('cache_config') || {};
			try {
				for (const key in object) {
					const element = object[key];
					// console.log(element, 'element')
					if (element.view_type == _this.page_type) {
						let { user_status = 0 } = _this.USER_MSG;

						if (_this.page_type == 'voltage_calibration') {
							let initialize_series_number = _this.BLUETOOTH_INIT_FORM['initialize_series_number'] || 14;
							let volage_list = [];
							for (let index = 1; index <= initialize_series_number; index++) {
								let volage_item = {
									view_tt: `第${index}串电压`,
									form_key: `voltage_calibration_${index}`,
									unit: 'mV',
									command_group_code: `${175 + index}`, // 0xb0 - 0xcf
								};
								volage_list.push(volage_item);
							}
							_this.$set(element, 'view_list', volage_list);
						}

						element.view_list.forEach(vv => {
							let _key = '--';
							let tips = '';
							if (_this.objJudge(vv.click_type)) {
								// switch select 暂时不需要
							} else {
								tips = `请输入${vv.view_tt}`;
							}
							if (vv.unit) {
								_key = `0`;
								// _key = `0${vv.unit}`
								tips += `（${vv.unit}）`;
							}

							if (_this.objJudge(tips)) {
								// 弹框input提示用
								_this.$set(vv, 'placeholder', tips);
							}

							// 二级过流和短路保护 值和延时值的设置和列表初始化
							if (_this.objJudge(cache_config)) {
								if (_this.objJudge(cache_config[vv.form_key])) {
									Object.assign(vv, cache_config[vv.form_key]);
								}
							}
							if (user_status == 2) {
								_this.$set(vv, 'can_click', false);
							}
						});
					}
				}
			} catch (error) {
			} finally {
				_this.com_loading_fun({ mode: 0 });
			}
		},

		// 弹窗点击
		popup_click(event) {
			let { type, item } = event;
			let _this = this;
			if (type == 1) {
				if (_this.objJudge(item.tips_key)) {
					_this.hidepopup();
				} else {
					// console.log(event);
					_this.COMMAND_TRANSFER(event);
				}
			} else {
				_this.hidepopup();
			}
		},
		openpopup(v) {
			this.$nextTick(() => {
				this.pagepopup = true;
			});
		},
		hidepopup() {
			let _this = this;
			this.pagepopup = false;
		},
	},
	watch: {},
};
</script>

<style lang="scss"></style>
